<template>
  <el-card shadow="never" class="aui-card--fill">
    <h3 v-if="showNo" style="margintop: 0">{{ $route.params.mtNo }}</h3>
    <div class="mod-wms__order-detail}">
      <wms-form v-model="dataForm" :formList="formList" :table-column="tableColumn" :advanced="advanced"
        @check-change="checkChange" @changeAdvance="changeAdvance" @getDataList="getDataList" @getReset="getReset">
        <el-form-item v-if="showNo">
          <el-button v-if="$hasPermission('wms:order-detail:save')" type="primary" @click="addOrUpdateHandle()">{{
      $t("add") }}</el-button>
          <el-button v-if="$hasPermission('wms:order-detail:auto-allot')" type="primary" @click="autoAllotHandle()">{{
      $t("autoAllot") }}</el-button>
          <el-button v-if="$hasPermission('wms:order-detail:refund-allot')" type="primary" @click="refundHandle()">{{
      $t("refund") }}</el-button>
          <el-button v-if="$hasPermission('wms:order-detail:manual-exec')" type="primary" @click="execHandle()">{{
      $t("orderExec") }}</el-button>
        </el-form-item>
      </wms-form>
      <wms-table ref="wmsTable" class="wmsTable" v-model="dataForm" :page="page" :total="total" :limit="limit"
        :tableData="dataList" :table-column="column" :module-options="mixinViewModuleOptions"
        :dataListLoading="dataListLoading" :showSelection="showNo" @deleteHandle="deleteHandle"
        @distributionHandle="distributionHandle" @allotDetailsHandle="allotDetailsHandle"
        @pageSizeChangeHandle="pageSizeChangeHandle" @pageCurrentChangeHandle="pageCurrentChangeHandle"
        @selection-change="handleSelectionChange" @addOrUpdateHandle="addOrUpdateHandle"/>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <manual-allot v-if="distributionVisable" ref="distribution" @refreshDataList="getDataList"></manual-allot>
      <allot-details v-if="allotDetailsVisable" ref="allotDetails" @refreshDataList="getDataList"></allot-details>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from './components/order-detail-add-or-update'
import manualAllot from "./components/distribution.vue";
import allotDetails from "./components/allot-details.vue";
import { formList, tableColumn } from "./config";
import wmsTable from "@/components/wms-table";
import wmsForm from "@/components/wms-form";
import { Upload } from "element-ui";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        createdIsNeed: false,
        getDataListURL: "/wms/order-detail/page",
        getDataListIsPage: true,
        exportURL: "/wms/order-detail/export",
        deleteURL: "/wms/order-detail",
        deleteIsBatch: true,
        deleteIsBatchKey: "detailId",
      },
      formList,
      tableColumn,
      showNo: false,
      detailId: '',
      dataForm: {
        detailId: "",
        orderId: "",
        spuNo:"",
        planQty:0,
        bindQty:0,
      },
      distributionVisable: false,
      allotDetailsVisable: false,
      ids: []
    };
  },
  components: {
    AddOrUpdate,
    manualAllot,
    allotDetails,
    wmsTable,
    wmsForm,
  },
  async query() {
    await this.getDataList(); // 确保数据加载完成
  },
  created() {
    if (this.$route.params.dtId) {
      this.showNo = true;
      this.dataForm.orderId = this.$route.params.dtId;
      this.dataForm.zllbh = this.$route.params.zllbh;
    }
    this.query();
  },
  methods: {
    // 单选
    handleSelectionChange(selection) {
      // this.detailId = ""; //每次单选都重置id
      // if (selection.length > 1) {
      //   this.$refs["wmsTable"].$refs.Table.clearSelection();
      //   this.$refs["wmsTable"].$refs.Table.toggleRowSelection(row);
      // }
      // if (selection.length != 0) {
      //   //只有当真正选中了目标主单才获取id
      //   this.detailId = row.detailId;
      // }
      this.ids = selection.map(item => item.detailId)
      console.log(this.ids,'isss');
    },
    distributionHandle(row) {
      console.log("Current row data:", row);
      this.distributionVisable = true;
      this.$nextTick(() => {
        this.$refs.distribution.init(row);
      });
    },
    allotDetailsHandle(row) {
      this.allotDetailsVisable = true;
      this.$nextTick(() => {
        // this.$refs.allotDetails.optionsParams.detailId = row.detailId
        this.$refs.allotDetails.init(row);
      })
    },
    autoAllotHandle() {
      if (this.ids.length != 1) {
        return this.$message({
          message: '请选择一条自动配货明细',
          type: "warning",
          duration: 1000,
        });
      }
      let id = this.ids[0]
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("outAllot") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      ).then(() => {
        this.$http
          .post("/wms/order-detail/auto-allot/" + id)
          .then(({ data: res }) => {
            if (res.code != 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: res.data,
              type: "success",
              duration: 1000,
              onClose: () => {
                this.getDataList();
              },
            });
          })
          .catch(() => { });
      })
        .catch(() => { });
    },
    //取消配货
    refundHandle() {
      if (this.ids.length != 1) {
        return this.$message({
          message: '请选择一条取消配货明细',
          type: "warning",
          duration: 1000,
        });
      }
      let id = this.ids[0]
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("refund") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      ).then(() => {
        this.$http
          .post("/wms/order-detail/refund-allot/" + id)
          .then(({ data: res }) => {
            if (res.code != 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: res.data,
              type: "success",
              duration: 1000,
              onClose: () => {
                this.getDataList();
              },
            });
          })
          .catch(() => { });
      })
        .catch(() => { });
    },
    execHandle() {
      if (!this.ids.length) {
        return this.$message({
          message: this.$t("prompt.outAllot"),
          type: "warning",
          duration: 1000,
        });
      }
      let ids = this.ids
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("orderExec") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      ).then(() => {
        this.$http
          .post("/wms/order-detail/manual-exec/", ids)
          .then(({ data: res }) => {
            if (res.code != 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: res.data,
              type: "success",
              duration: 1000,
              onClose: () => {
                this.getDataList();
              },
            });
          })
          .catch(() => { });
      })
        .catch(() => { });
    }
  },
};
</script>
<style scoped>
/* 隐藏表头的全选框 */
/*::v-deep .wmsTable thead .el-table-column--selection .cell {
  display: none;
}*/
</style>
